from urllib import request, parse
from bs4 import BeautifulSoup
from sqlalchemy import create_engine, Column, String, DateTime, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import time

Base = declarative_base()


class CourseDirect(Base):
    __tablename__ = 'course_direct'
    id = Column(Integer, primary_key=True)

    direct_name = Column(String(50))
    direct_url = Column(String(255))
    direct_code = Column(String(20))
    created_at = Column(DateTime)


class CourseType(Base):
    __tablename__ = 'course_type'
    id = Column(Integer, primary_key=True)
    course_direct_id = Column(Integer)
    course_type_name = Column(String(50))
    course_type_url = Column(String(255))
    course_type_code = Column(String(20))
    created_at = Column(DateTime)


class Course(Base):
    __tablename__ = 'course_list'
    id = Column(Integer, primary_key=True)
    course_type_id = Column(Integer)
    course_url = Column(String(255))
    course_img_url = Column(String(255))
    course_teacher_name = Column(String(50))
    course_name = Column(String(255))
    course_level = Column(String(50))
    course_students_num = Column(Integer)
    course_desc = Column(String(255))
    course_code_num = Column(Integer)
    course_label = Column(String(255))
    created_at = Column(DateTime)


def now_date_time():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


def get_html(url):
    res = request.urlopen(url)
    return res.read().decode('utf-8')


def get_course_direct(url):
    html = get_html(url)
    soup = BeautifulSoup(html, 'html5lib')
    course_direct_soup = soup.find_all(class_='course-nav-row')[0]
    directs = course_direct_soup.find_all('a')
    course_direct_arr = []
    for direct in directs:
        if direct.text != '全部':
            course_direct_arr.append({
                'direct_name': direct.text,
                'direct_url': parse.urljoin(url, direct['href'])
            })
    # print(course_direct_arr)
    return course_direct_arr


def get_direct_type(direct_url):
    res_html = get_html(direct_url)
    soup = BeautifulSoup(res_html, 'html5lib')
    source_types = soup.find_all(class_='course-nav-row')[1]
    types = source_types.find_all('a')
    types_arr = []
    for c_type in types:
        if c_type.text != '全部':
            types_arr.append({
                'type_name': c_type.text,
                'type_url': parse.urljoin(direct_url, c_type['href'])
            })
    # print(types_arr)
    return types_arr


def get_session():
    engine = create_engine('mysql+pymysql://imooc:imooc@127.0.0.1:3306/imooc')
    Session = sessionmaker(bind=engine)
    return Session()


def insert_direct(name, url):
    session = get_session()
    ad_course_direct = CourseDirect(
        id=None,
        direct_name=name,
        direct_url=url,
        direct_code=get_type_code(url),
        created_at=now_date_time()
    )
    session.add(ad_course_direct)
    session.commit()
    return ad_course_direct.id


def insert_type(course_direct_id, name, url):
    session = get_session()
    ad_course_type = CourseType(
        id=None,
        course_direct_id=course_direct_id,
        course_type_name=name,
        course_type_url=url,
        course_type_code=get_type_code(url),
        created_at=now_date_time()
    )
    session.add(ad_course_type)
    session.commit()
    return ad_course_type.id


def insert_course(course_type_id, course_list_infos):
    session = get_session()
    all_data = []
    for course_list in course_list_infos:
        all_data.append(Course(
            id=None,
            course_type_id=course_type_id,
            course_url=course_list['course_url'],
            course_img_url=course_list['course_img_url'],
            course_teacher_name=course_list['course_teacher_name'],
            course_name=course_list['course_name'],
            course_level=course_list['course_level'],
            course_students_num=course_list['course_students_num'],
            course_desc=course_list['course_desc'],
            course_label=','.join(course_list['course_label']),
            course_code_num=get_course_num(course_list['course_url']),
            created_at=now_date_time(),

        ))
        session.add_all(all_data)
        session.commit()


def get_course_list(type_url):
    course_lists_arr = []
    while type_url:
        res_html = get_html(type_url)
        soup = BeautifulSoup(res_html, 'html5lib')
        course_lists_soup = soup.find_all(class_='course-card-container')
        for course_list in course_lists_soup:
            course_url = parse.urljoin(type_url, course_list.find(class_='course-card')['href'])
            course_img_url = parse.urljoin(type_url,
                                           course_list.find(class_='course-card-top').find('img')['data-original'])
            # print(course_list.find(class_='teacher-name'))
            # course_teacher_name = course_list.find(class_='teacher-name').text
            course_teacher_soup = course_list.find(class_='teacher-name')
            if course_teacher_soup:
                course_teacher_name = course_teacher_soup.text
            else:
                course_teacher_name = ''

            course_label_soup = course_list.find(class_='course-label')
            course_label_arr = []
            if course_label_soup:
                course_labels = course_label_soup.find_all('label')
                for course_label in course_labels:
                    course_label_arr.append(course_label.text)

            course_name = course_list.find(class_='course-card-name').text
            course_info = course_list.find(class_='course-card-info').find_all('span')
            course_level = course_info[0].text
            course_students_num = course_info[1].text
            course_desc = course_list.find(class_='course-card-desc').text
            course_infos = {
                'course_url': course_url,
                'course_img_url': course_img_url,
                'course_teacher_name': course_teacher_name,
                'course_name': course_name,
                'course_level': course_level,
                'course_students_num': course_students_num,
                'course_desc': course_desc,
                'course_label': course_label_arr
            }
            course_lists_arr.append(course_infos)
        type_url = get_next_page_url(soup, type_url)
    return course_lists_arr


def get_next_page_url(soup, type_url):
    next_page_soup = soup.find('a', text='下一页')
    next_page_url = ''
    if next_page_soup:
        next_page_url = parse.urljoin(type_url, next_page_soup['href'])
    return next_page_url


def get_type_code(type_url):
    return type_url.query.split('=')[-1:][0]


def get_course_num(course_url):
    return int(course_url.path.strip('/').split('/')[-1:][0])


def get_chapter_list(url):
    res_html = get_html(url)
    soup = BeautifulSoup(res_html, 'html5lib')
    chapter_lists_soup = soup.find_all(class_='chapter')
    for chapter_list_soup in chapter_lists_soup:
        print(chapter_list_soup.find('h3').text.strip())
        print(chapter_list_soup.find(class_='chapter-description').text.strip())

        for chapter_url_soup in chapter_list_soup.find_all('a'):
            # print(chapter_url_soup.contents[2])
            # print(chapter_url_soup.text.strip().replace('开始学习', '').replace('  ', '').replace('\n', ''))
            print(chapter_url_soup.text.replace('开始学习', '').replace('  ', '').strip().split('\n'))


if __name__ == '__main__':
    url = 'https://www.imooc.com/learn/1157'
    get_chapter_list(url)

    # start_url = 'https://www.imooc.com/course/list'
    #
    # curse_directs = get_course_direct(start_url)
    # course_direct_num = 0
    # # 获取方向列表
    # for course_direct in curse_directs:
    #     course_direct_num += 1
    #     direct_id = insert_direct(course_direct['direct_name'], course_direct['direct_url'])
    #     course_types = get_direct_type(course_direct['direct_url'])
    #     course_type_num = 0
    #     # 获取分类列表
    #     for course_type in course_types:
    #         course_type_num += 1
    #         type_id = insert_type(direct_id, course_type['type_name'], course_type['type_url'])
    #         # 获取课程列表
    #         course_lists = get_course_list(course_type['type_url'])
    #         for course in course_lists:
    #             # https: // www.imooc.com / learn / 1157
    #             # 获取课程章节地址
    #             pass
    #         # insert_course(type_id, course_lists)
    #         # print('方向:', '[', course_direct_num, '/', len(curse_directs), ']:', course_direct['direct_name'], '; ',
    #         #       '类型:', '[', course_type_num, '/', len(course_types), ']:', course_type['type_name'], '; ')
